﻿using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

namespace imvs
{
    public static class StringUtils
    {
        // 字符串分割（正则实现）
        public static List<string> StringSplit(string input, string delimiter)
        {
            string pattern = $@"([^{Regex.Escape(delimiter)}]+)";
            var matches = Regex.Matches(input, pattern);

            var result = new List<string>(matches.Count);
            foreach (Match match in matches)
            {
                if (match.Success)
                    result.Add(match.Value);
            }
            return result;
        }

        // 字符串连接
        public static string StringJoin(IEnumerable<object> list, string separator)
        {
            if (list == null) return string.Empty;

            var sb = new StringBuilder();
            bool first = true;
            foreach (var item in list)
            {
                if (!first) sb.Append(separator);
                sb.Append(item?.ToString() ?? "");
                first = false;
            }
            return sb.ToString();
        }

        // 列表转字符串
        public static string ListToString(IEnumerable<object> list, string separator)
        {
            var strList = new List<string>();
            foreach (var item in list)
            {
                strList.Add(item?.ToString() ?? "");
            }
            return StringJoin(strList, separator);
        }

        public static string PointToString(Point3d pt, int epsilon = 3)
        {
            if (epsilon < 0) epsilon = 3;
            //return pt.ToString($"F{epsilon}", CultureInfo.CurrentCulture);
            return $"{pt.X:F3}_{pt.Y:F3}_{pt.Z:F3}";
        }

        // 字符串转浮点数（带格式校验）
        public static double? StringToFloat(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
                return null;

            // 清理非法字符
            var cleaned = new StringBuilder();
            foreach (char c in input.Trim())
            {
                if (char.IsDigit(c) || c == '.' || c == ',' ||
                    c == 'e' || c == 'E' || c == '-')
                {
                    cleaned.Append(c);
                }
            }
            string numStr = cleaned.ToString()
                .Replace(',', '.'); // 统一小数点格式

            // 验证数字格式
            string[] parts = numStr.Split(new[] { 'e', 'E' }, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length > 2) return null; // 多个指数符号

            int dotCount = parts[0].Split('.').Length - 1;
            if (dotCount > 1) return null; // 多个小数点

            // 尝试转换
            if (double.TryParse(numStr,
                System.Globalization.NumberStyles.Float,
                System.Globalization.CultureInfo.InvariantCulture,
                out double result))
            {
                return result;
            }
            return null;
        }
    }


}
